import Taro from '@tarojs/taro'
import app from '../../../../main.js'
let myJs = {
    getData: function() {
        this.state = {
            texaShow:true,
            template: {
                id: this.$router.params.id,
                type: this.$router.params.type,
                title: '',
                date_type: '就诊',
                items: [
                    {
                        time_type: '天',
                        amount: 1,
                        content: []
                    }
                ]
            }
        };
        return this.state;
    },
    toBack() {
        Taro.navigateBack({delta:1})       
    },
    getTemplate() {
        var me = this;
        app.get('web/flow_template/get', {id: this.state.template.id}, function(res) {
            me.setState({
                template: res.data.result
            });
        });
    },
    updateTemplate() {
        var me = this;
        var template = me.state.template;
        if (app.isEmpty(template.title)) {
            app.toast('请输入标题');
            return;
        }
        template.items = JSON.stringify(me.state.template.items);
        app.post('web/flow_template/update', this.state.template, function(res) {
            if (res.data.status == 200) {
                app.success('保存成功', function(res) {
                    setTimeout(me.toBack, 1000);
                });
            } else {
                app.toast('保存失败');
            }
        });
    },
    createTemplate() {
        var me = this;
        var template = me.state.template;
        if (app.isEmpty(template.title)) {
            app.toast('请输入标题');
            return;
        }
        template.items = JSON.stringify(me.state.template.items)
        app.post('web/flow_template/create', this.state.template, function(res) {
            if (res.data.status == 200) {
                app.success('保存成功', function(res) {
                    setTimeout(me.toBack, 1000);
                });
            } else {
                app.toast('保存失败');
            }
        });
    },
    copyTemplate() {
        var me = this;
        var template = me.state.template;
        if (app.isEmpty(template.title)) {
            app.toast('请输入标题');
            return;
        }
        template.items = JSON.stringify(me.state.template.items);
        app.post('web/flow_template/copy', this.state.template, function(res) {
            if (res.data.status == 200) {
                app.success('保存成功', function(res) {
                    setTimeout(me.toBack, 1000);
                });
            } else {
                app.toast('保存失败');
            }
        });
    },
    deleteTemplate() {
        var me = this;
        app.isConfirm('是否要删除该计划', function() {
            app.post('web/flow_template/delete', {id: me.state.template.id}, function(res) {
                if (res.data.status == 200) {
                    app.success('删除成功', function(res) {
                        setTimeout(me.toBack, 1000);
                    });
                } else {
                    app.toast('删除失败');
                }
            });
        });
    },
    componentWillMount () {
        
    },
    componentDidShow () {
        if(this.init) {
            this.init();
        }
    },
    navigateTo(url) {
        app.navigateTo(url);
    },
    handleChange(name, v) {
        var names = name.split('.');
        var n = names[0];
        var temp = this.state;
        for(var i = 0; i < names.length - 1; i++) {
            temp = temp[names[i]];
        }
        temp[names[i]] = v;
        this.setState({n: temp[names[i]]});
    },
    handleTextareaChange(name, e) {
        var names = name.split('.');
        var n = names[0];
        var temp = this.state;
        for(var i = 0; i < names.length - 1; i++) {
            temp = temp[names[i]];
        }
        temp[names[i]] = e.target.value;
        this.setState({n: temp[names[i]]});
    },
    redirectTo(url) {
        app.redirectTo(url);
    },
    componentDidMount () {},
    componentWillUnmount () {},
    componentDidHide () {}
}
export default myJs;
